!(function (window) {
	var $form = document.querySelector('#form')
	var emailModel = { name: "email", message: "请输入有效的邮箱地址", validate: emailValidate }
	var nameModel = { name: "username", message: "请输入用户名，字母数字下划线 1 到 10 位，不能是数字开头", validate: nameValidate }
	var passModel = { name: "password", message: "请输入密码，密码请设置 6 - 16 位字符", validate: passValidate }
	var repassModel = { name: "repassword", message: "请输入相同的密码", validate: repassValidate }
	var phoneModel = { name: "telephone", message: "请输入有效的手机号码", validate: phoneValidate }
	var formModels = [emailModel, nameModel, passModel, repassModel, phoneModel]
	
	function emailValidate (val) {
		return (/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/).test(val)
	}
	function nameValidate (val) {
		return (/^(?![0-9])\w{1,10}$/).test(val)
	}
	function passValidate (val) {
		return (/.{6,16}$/).test(val)
	}
	function repassValidate (cur, val) {
		return cur === val
	}
	function phoneValidate (val) {
		return (/^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/).test(val)
	}
	
	window.onSubmit = function () {
		var $pass = $form.querySelector('[name="'+ passModel.name +'"]')
		
		for (var i = 0; i < formModels.length; i++) {
			var model = formModels[i]
			var $el = $form.querySelector('[name="'+ model.name +'"]')
			
			if (model.name === 'repassword' && typeof model.validate === 'function' && model.validate($el.value, $pass.value)) continue;
			if (typeof model.validate === 'function' && model.validate($el.value)) continue;
			
			alert(model.message)
			
			return false
		}
		
		return true
	}
})(window);